home *** CD-ROM | disk | FTP | other *** search
- Path: news.bu.edu!engc!jayaram
- From: jayaram@engc.bu.edu (J. Ram)
- Newsgroups: comp.lang.c
- Subject: Pascal -> C conversion
- Date: 17 Apr 1996 21:54:43 GMT
- Organization: Boston University
- Message-ID: <4l3pb3$skd@news.bu.edu>
- NNTP-Posting-Host: engc.bu.edu
- X-Newsreader: TIN [version 1.2 PL0]
-
-
- Hello netters:
-
- I have a simple 23 line pascal program I wrote 5 years ago that
- I'd like to CONVERT to C. Can someone help since I'm not well
- versed with the C syntax. All that needs to be done is change of syntax
- without any change in programming style or approach to the problem.
-
- This pascal program uses a simple circular linked lists to solve the "Josephus
- Problem".
-
- Start of program
-
- ----------------------------------------------------------------------------
- August 25, 1991
-
- The program is a short program that solves the "Josephus Problem"
- For the Josephus problem, we can imagine that N people have decided to commit
- mass suicide by arranging themselves in circle and killing the M th person
- around the circle, closing ranks as each person drops out of the circle.
- The problem is to find out which person is the last to die or more importantly
- to find the order in which the people were executed.
-
- Example
- for N=9 and M=5, the order of execution is 5,1,7,4,3,6,9,2,8
-
- --------------------------PASCAL PROGRAM -----
- PROGRAM josephus(input,output);
- type link = ^node;
- node = RECORD key:integer;
- next:link
- END;
- var i,N,M:integer;
- t,x:link;
- begin
- read (N,M);
- new(t); t^.key:=1; x:=t;
- for i:=2 to N do
- begin
- new(t^.next); t:=t^.next; t^.key:= i
- end;
- t^.next:=x;
- while t <> t^.next do
- begin
- for i:=1 to M-1 do t:=t^.next;
- write(t^.next^.key);
- x:=t^.next;
- t^.next:=t^.next^.next;
- dispose(x);
- end;
- writeln(t^.key);
- end
-
- -------end program-------------------------------
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-